let showDivs = [];
let mutiModel = false;
let oldSelection = null;
function searchTimestamp() {
	console.log("timestamp to date plugin working");
	window.onmouseup = function({pageX, pageY}){
	    let selection = document.getSelection();
	    if (selection.type === "Range") {
	    	// console.log(selection);
		    try{
			    let text = selection.baseNode.data.substring(selection.baseOffset, selection.focusOffset);
			    if (text.length < 13) {
			    	text += "000";
			    }
			    let ts = parseInt(text);
			    if (ts > 1000000000000 && ts < 2000000000000) {
			    	let date = new Date(ts);
			    	let dateS = date2format(date); 
			    	console.log(dateS);
			    	showHtml(dateS, pageX, pageY);
			    }
			} catch(e){
				console.log(e);
			} finally {

			}
	    } else {
	    	if (!mutiModel) {
	    		for(let d of showDivs) {
	    			d.remove();
	    		}
	    		showDivs = [];
	    	}
		    
	    }
	}
	window.onkeydown = (event) => {
		// console.log(event)
		// Control_L：17
		// h : 72
		if(event.keyCode == 72) {
			mutiModel = true;
		}
	}
	window.onkeyup = (event) => {
		// Control_L
		if(event.keyCode == 72) {
			mutiModel = false;
		}
	}
}
function date2format(date) {
	return (1900 + date.getYear()) + "-" + (date.getMonth() + 1) + "-" + (date.getDate()) + " " + date.getHours() + ":" + date.getMinutes() + ":" + date.getSeconds() + "." + date.getMilliseconds();
}
function showHtml(dateString, x, y) {
	//新建一个div元素节点
	let showDiv = document.createElement("div");
	showDiv.style = `position: absolute; 
		z-index: 999999;
	    left: ${x}px; 
	    top: ${y}px;
	    background-color:#fff;
	    color:#000;
	    padding: 2px 14px;
	    border-radius: 6px;
	    box-shadow: 0 0 4px 0px #000;`;
	showDiv.onmouseup = (e)=> {
		e.stopPropagation();
	}
	showDiv.innerText = dateString;
	//把div元素节点添加到body元素节点中成为其子节点，但是放在body的现有子节点的最后
	document.body.appendChild(showDiv);
	showDivs.push(showDiv);
}
searchTimestamp();

function clearPreFlag() {
	let a = document.getElementsByClassName("isPre");
	if (a.length>0) {
		document.body.removeChild(a[0])
	}
}
clearPreFlag();

function oneKeyRead() {
	for(o of document.getElementsByClassName("mark-read-btn-unread")) {
	    o.click();
	}
}
